<template>
        <common-layout>
                <div class="top">
                        <div class="header">
                                <img alt="logo" class="logo" src="@/assets/img/logo.png"/>
                                <span class="title">{{systemName}}</span>
                        </div>
                        <div class="desc">李成龙开发练手框架</div>
                </div>
                <div class="login">
                        <a-form @submit="onSubmit" :form="form">
                                <a-tabs size="large" :tabBarStyle="{textAlign: 'center'}" style="padding: 0 2px;">
                                        <a-tab-pane tab="账户密码登录" key="1">
                                                <a-alert type="error" :closable="true" v-show="error" :message="error"
                                                         showIcon style="margin-bottom: 24px;"/>
                                                <a-form-item>
                                                        <a-input
                                                                autocomplete="off"
                                                                size="large"
                                                                placeholder="请输入账户名"
                                                                v-decorator="['username', {rules: [{ required: true, message: '请输入账户名', whitespace: true}]}]"
                                                        >
                                                                <a-icon slot="prefix" type="user"/>
                                                        </a-input>
                                                </a-form-item>
                                                <a-form-item>
                                                        <a-input
                                                                size="large"
                                                                placeholder="请输入密码"
                                                                autocomplete="off"
                                                                type="password"
                                                                v-decorator="['password', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
                                                        >
                                                                <a-icon slot="prefix" type="lock"/>
                                                        </a-input>
                                                </a-form-item>
                                                <a-form-item>
                                                        <a-row :gutter="8" style="margin: 0 -4px">
                                                                <a-col :span="16">
                                                                        <a-input
                                                                                autocomplete="off"
                                                                                size="large"
                                                                                placeholder="请输入验证码"
                                                                                v-decorator="['captcha', {rules: [{ required: true, message: '请输入验证码', whitespace: true}]}]"
                                                                        >
                                                                                <a-icon slot="prefix" type="code"/>
                                                                        </a-input>
                                                                </a-col>
                                                                <a-col :span="8" style="padding-left: 4px;">
                                                                        <img v-on:click="loadPicCaptcha()"
                                                                             :src="captcha" height="39" width="99%"
                                                                             style="vertical-align: top;border-radius:5px;padding-top:2px;cursor:pointer;"/>
                                                                </a-col>
                                                        </a-row>
                                                </a-form-item>
                                        </a-tab-pane>
                                        <a-tab-pane tab="手机号登录" key="2">
                                                <a-form-item>
                                                        <a-input size="large" placeholder="暂不支持手机号登录">
                                                                <a-icon slot="prefix" type="mobile"/>
                                                        </a-input>
                                                </a-form-item>
                                                <a-form-item>
                                                        <a-row :gutter="8" style="margin: 0 -4px">
                                                                <a-col :span="16">
                                                                        <a-input size="large" placeholder="动态验证码">
                                                                                <a-icon slot="prefix" type="mail"/>
                                                                        </a-input>
                                                                </a-col>
                                                                <a-col :span="8" style="padding-left: 4px">
                                                                        <a-button style="width: 100%"
                                                                                  class="captcha-button" size="large">
                                                                                获取验证码
                                                                        </a-button>
                                                                </a-col>
                                                        </a-row>
                                                </a-form-item>
                                        </a-tab-pane>
                                </a-tabs>
                                <!--<div>
                                        <a-checkbox :checked="true">自动登录</a-checkbox>
                                        <a style="float: right">忘记密码</a>
                                </div>-->
                                <a-form-item>
                                        <a-button :loading="logging" style="width: 100%;margin-top: 24px" size="large"
                                                  htmlType="submit" type="primary">登录
                                        </a-button>
                                </a-form-item>
                                <!--<div>
                                        其他登录方式
                                        <a-icon class="icon" type="alipay-circle"/>
                                        <a-icon class="icon" type="taobao-circle"/>
                                        <a-icon class="icon" type="weibo-circle"/>
                                        <router-link style="float: right" to="/dashboard/workplace">注册账户</router-link>
                                </div>-->
                        </a-form>
                </div>
        </common-layout>
</template>

<script>
        import CommonLayout from '@/layouts/CommonLayout';
        import {login} from '@/services/user';
        import {captcha} from '@/services/common';
        import {setAuthorization} from '@/utils/request';
        import {mapMutations} from 'vuex';
        import {loadPersonalAfterLoginRoutes} from '@/utils/routerUtil';

        export default {
                name: 'Login',
                components: {CommonLayout},
                data() {
                        return {
                                logging: false,
                                error: '',
                                form: this.$form.createForm(this),
                                captcha: '',
                                key: ''
                        }
                },
                created() {
                        this.loadPicCaptcha();
                },
                computed: {
                        systemName() {
                                return this.$store.state.setting.systemName;
                        }
                },
                methods: {
                        ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
                        /**
                         * 提交
                         */
                        onSubmit(e) {
                                var that = this;
                                e.preventDefault();
                                that.form.validateFields((err) => {
                                        if (!err) {
                                                that.logging = true;
                                                const username = that.form.getFieldValue('username');
                                                const password = that.form.getFieldValue('password');
                                                const captcha = that.form.getFieldValue('captcha');
                                                const key = that.key;
                                                login(username, password, captcha, key).then(function (result) {
                                                        if (result.data.code !== 0) {
                                                                that.logging = false;
                                                                that.$message.error(result.data.message, 3);
                                                                that.loadPicCaptcha();
                                                                return false;
                                                        }
                                                        that.setUser(result.data.data.user);

                                                        setAuthorization({
                                                                token: result.data.data.token,
                                                                //expireAt: new Date(loginRes.data.expireAt)
                                                        });
                                                        loadPersonalAfterLoginRoutes(function () {
                                                                that.$message.success(result.data.message, 3);
                                                                that.logging = false;
                                                                that.$router.push('/dashboard/workplace');
                                                        }, function (error) {
                                                                that.$message.error(error.response.data, 3);
                                                                that.logging = false;
                                                                that.loadPicCaptcha();
                                                        });
                                                }).catch(function (error) {
                                                        that.$message.error(error.response.data, 3);
                                                        that.logging = false;
                                                        that.loadPicCaptcha();
                                                });
                                        }
                                });
                        },
                        /**
                         * 加载图片验证码
                         */
                        loadPicCaptcha() {
                                var that = this;
                                captcha(100, 30).then(function (result) {
                                        if (result.data.code != 0) {
                                                that.$message.error(result.data.message, 3);
                                                that.logging = false;
                                                return false;
                                        }
                                        that.$set(that, 'captcha', result.data.data.captcha);
                                        that.$set(that, 'key', result.data.data.key);
                                }).catch(function (err) {
                                        that.$message.error(err.response.data, 3);
                                        that.logging = false;
                                });
                        }
                }
        }
</script>

<style lang="less" scoped>
        .common-layout {
                .top {
                        text-align: center;

                        .header {
                                height: 44px;
                                line-height: 44px;

                                a {
                                        text-decoration: none;
                                }

                                .logo {
                                        height: 44px;
                                        vertical-align: top;
                                        margin-right: 16px;
                                }

                                .title {
                                        font-size: 33px;
                                        color: @title-color;
                                        font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
                                        font-weight: 600;
                                        position: relative;
                                        top: 2px;
                                }
                        }

                        .desc {
                                font-size: 14px;
                                color: @text-color-second;
                                margin-top: 12px;
                                margin-bottom: 40px;
                        }
                }

                .login {
                        width: 368px;
                        margin: 0 auto;
                        @media screen and (max-width: 576px) {
                                width: 95%;
                        }
                        @media screen and (max-width: 320px) {
                                .captcha-button {
                                        font-size: 14px;
                                }
                        }

                        .icon {
                                font-size: 24px;
                                color: @text-color-second;
                                margin-left: 16px;
                                vertical-align: middle;
                                cursor: pointer;
                                transition: color 0.3s;

                                &:hover {
                                        color: @primary-color;
                                }
                        }
                }
        }
</style>
